<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules">
      <!-- <p>{{ `输入以下内容以确认:${content}` }}</p> -->
      <p>输入以下内容以确认：<strong style="font-size: 16px;">{{ content }}</strong></p>
      <p style="color: red;">删除字段会导致使用该字段的模板显示异常，请谨慎操作</p>
      <el-form-item prop="content">
        <el-input v-model="formData.content" placeholder="请输入字段名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
defineOptions({ name: 'Lock' })
import * as ProjectApi from '@/api/project'
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('提示') // 弹窗的标题
const formLoading = ref(false)
const formData = ref({} as any)
const formRef = ref()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const content = ref('')
const id = ref('')
const { currentRoute } = useRouter() // 路由对象
const { query } = unref(currentRoute)
const validate = (rule, value, callback) => {
  if (value != content.value) {
    callback(new Error('输入内容不正确'));
  } else {
    callback();
  }
}
const formRules = reactive({
  content: [{ validator: validate, trigger: 'blur' }]
})
/** 打开弹窗 */
const open = (newId, newContent) => {
  dialogVisible.value = true
  content.value = newContent
  id.value = newId
  resetForm()
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: id.value,
    content: '',
  }
  formRef.value?.resetFields()
}
defineExpose({ open })

/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  formLoading.value = true
  try {
    // 发起删除
    await ProjectApi.deleteProjectModule({ id: id.value, projectId: query.projectId })
    message.success(t('common.delSuccess'))
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
